package com.b2.service.impl;

import com.b2.constant.MessageConstant;
import com.b2.context.BaseContext;
import com.b2.mapper.*;
import com.b2.pojo.dto.addClassDto;
import com.b2.pojo.entity.Class;
import com.b2.pojo.entity.*;
import com.b2.pojo.vo.ClassOfDirectorVo;
import com.b2.pojo.vo.ClassOfParentVo;
import com.b2.pojo.vo.ClassOfTeacherVo;
import com.b2.service.ClassService;
import com.b2.utils.AliOssUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author 悦神
* @description 针对表【class】的数据库操作Service实现
* @createDate 2024-04-25 14:27:22
*/
@Service
@Transactional
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class>
    implements ClassService{

    @Autowired
    UserMapper userMapper;
    @Autowired
    DirectorMapper directorMapper;
    @Autowired
    ClassMapper classMapper;
    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    ParentMapper parentMapper;
    @Autowired
    ParentClassMapper parentClassMapper;

    @Autowired
    ParentVideoMapper parentVideoMapper;

    @Autowired
    AliOssUtil aliOssUtil;
    @Override
    @Transactional
    public Class addclass(addClassDto classDto) {
        String name = classDto.getName();
        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        String phone = user.getPhone();
        LambdaQueryWrapper<Director> o = new LambdaQueryWrapper<>();
        o.eq(Director::getPhone,phone);
        Director director = directorMapper.selectOne(o);
        Long id = director.getId();
        Class aClass = new Class();
        aClass.setName(name);
        aClass.setDirectorId(id);
        classMapper.insert(aClass);
        Long id1 = aClass.getId();
        String code1=Long.toString(id1+1000L);
        String code2=Long.toString(id1+10000L);
        aClass.setCode1(code1);
        aClass.setCode2(code2);
        classMapper.updateById(aClass);
        return aClass;
    }

    @Override
    public List<ClassOfDirectorVo> getclass1() {
        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        String phone = user.getPhone();
        LambdaQueryWrapper<Director> o = new LambdaQueryWrapper<>();
        o.eq(Director::getPhone,phone);
        Director director = directorMapper.selectOne(o);
        Long id = director.getId();
        LambdaQueryWrapper<Class> o1 = new LambdaQueryWrapper<>();
        o1.eq(Class::getDirectorId,id);
        List<Class> classes = classMapper.selectList(o1);
        List<ClassOfDirectorVo> list = new ArrayList<>();
        for (Class c:classes) {
            Long teacherId = c.getTeacherId();
            ClassOfDirectorVo classOfDirectorVo = new ClassOfDirectorVo();
            if (teacherId!=null) {
                Teacher teacher = teacherMapper.selectById(teacherId);
                classOfDirectorVo.setTeacher_name(teacher.getName());
            }
            classOfDirectorVo.setCode1(c.getCode1());
            classOfDirectorVo.setCode2(c.getCode2());
            classOfDirectorVo.setName(c.getName());
            classOfDirectorVo.setId(c.getId());
            list.add(classOfDirectorVo);
        }
        return list;
    }

    @Override
    public List<ClassOfParentVo> getclass3() {
        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        String phone = user.getPhone();
        LambdaQueryWrapper<Parent> o = new LambdaQueryWrapper<>();
        o.eq(Parent::getPhone,phone);
        Parent parent = parentMapper.selectOne(o);
        Long id = parent.getId();
        LambdaQueryWrapper<ParentClass> o1 = new LambdaQueryWrapper<>();
        o1.eq(ParentClass::getParentId,id);
        List<ParentClass> parentClasses = parentClassMapper.selectList(o1);
        List<ClassOfParentVo> lists = new ArrayList<>();
        for (ParentClass parentClasse:parentClasses)
        {
            Long classId = parentClasse.getClassId();
            Class aClass = classMapper.selectById(classId);
            Long teacherId = aClass.getTeacherId();
            Teacher teacher = teacherMapper.selectById(teacherId);
            ClassOfParentVo classOfParentVo = new ClassOfParentVo();
            classOfParentVo.setName(aClass.getName());
            classOfParentVo.setTeacher_name(teacher.getName());
            classOfParentVo.setId(aClass.getId());
            lists.add(classOfParentVo);
        }
        return lists;
    }

    @Override
    public List<ClassOfTeacherVo> getclass2() {
        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        String phone = user.getPhone();
        LambdaQueryWrapper<Teacher> o = new LambdaQueryWrapper<>();
        o.eq(Teacher::getPhone,phone);
        Teacher teacher = teacherMapper.selectOne(o);
        Long id = teacher.getId();
        LambdaQueryWrapper<Class> o1 = new LambdaQueryWrapper<>();
        o1.eq(Class::getTeacherId,id);
        List<Class> classes = classMapper.selectList(o1);
        List<ClassOfTeacherVo> list = new ArrayList<>();
        for (Class classe: classes) {
            ClassOfTeacherVo classOfTeacherVo = new ClassOfTeacherVo();
            classOfTeacherVo.setName(classe.getName());
            classOfTeacherVo.setCode2(classe.getCode2());
            classOfTeacherVo.setTeacher_name(teacher.getName());
            classOfTeacherVo.setId(classe.getId());
            list.add(classOfTeacherVo);
        }
       return list;
    }

    @Override
    public List<Parent> getParentInfomation(Long id) {
        LambdaQueryWrapper<ParentClass> o = new LambdaQueryWrapper<>();
        o.eq(ParentClass::getClassId,id);
        List<ParentClass> parentClasses = parentClassMapper.selectList(o);
        List<Parent> lists = new ArrayList<>();
        for (ParentClass p:parentClasses) {
            Long parentId = p.getParentId();
            Parent parent = parentMapper.selectById(parentId);
            lists.add(parent);
        }
        return lists;
    }
    @Transactional
    @Override
    public void TeacherToClass(String code) {
        LambdaQueryWrapper<Class> o = new LambdaQueryWrapper<>();
        o.eq(Class::getCode1, code);
        Class aClass = classMapper.selectOne(o);
        if (aClass==null) {
            throw new RuntimeException(MessageConstant.CODE_FAILED);
        }
        if (aClass.getTeacherId()!=null) {
            throw  new RuntimeException(MessageConstant.ADDCLASS_FAILED);
        }
        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        String phone = user.getPhone();
        LambdaQueryWrapper<Teacher> o1 = new LambdaQueryWrapper<>();
        o1.eq(Teacher::getPhone,phone);
        Teacher teacher = teacherMapper.selectOne(o1);
        Long id = teacher.getId();
        aClass.setTeacherId(id);
        classMapper.updateById(aClass);
    }
    @Transactional
    @Override
    public void ParentToClass(String code) {
        LambdaQueryWrapper<Class> o = new LambdaQueryWrapper<>();
        o.eq(Class::getCode2, code);
        Class aClass = classMapper.selectOne(o);
        if (aClass==null) {
            throw new RuntimeException(MessageConstant.CODE_FAILED);
        }
        if (aClass.getTeacherId()==null) {
            throw  new RuntimeException(MessageConstant.VOID_TEACHER);
        }

        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        String phone = user.getPhone();
        LambdaQueryWrapper<Parent> o1 = new LambdaQueryWrapper<>();
        o1.eq(Parent::getPhone,phone);
        Parent parent = parentMapper.selectOne(o1);
        Long id = parent.getId();

        LambdaQueryWrapper<ParentClass> p = new LambdaQueryWrapper<>();
        p.eq(ParentClass::getClassId,aClass.getId());
        p.eq(ParentClass::getParentId,id);
        List<ParentClass> parentClasses = parentClassMapper.selectList(p);
        if (parentClasses.size()>0) {
            throw new RuntimeException(MessageConstant.CLASS_ENTER);
        }
        ParentClass parentClass = new ParentClass();
        parentClass.setParentId(id);
        parentClass.setClassId(aClass.getId());
        parentClassMapper.insert(parentClass);
    }
    @Transactional
    @Override
    public void deleteclass(Long id) {
        LambdaQueryWrapper<ParentClass> o = new LambdaQueryWrapper<>();
        classMapper.deleteById(id);
        o.eq(ParentClass::getClassId,id);
        parentClassMapper.delete(o);
        LambdaQueryWrapper<ParentVideo> p = new LambdaQueryWrapper<>();
        p.eq(ParentVideo::getClassId,id);
        List<ParentVideo> parentVideos = parentVideoMapper.selectList(p);
        for (ParentVideo parentVideo:parentVideos) {
            if (parentVideo.getParentVideoUrl()!=null) {
                aliOssUtil.deleteFile(parentVideo.getParentVideoUrl());
            }
            if (parentVideo.getTeacherPictureUrl()!=null) {
                aliOssUtil.deleteFile(parentVideo.getTeacherPictureUrl());
            }
        }
        parentVideoMapper.delete(p);

    }

    @Override
    @Transactional
    public void parent_exit_class(Long id) {
        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        String phone = user.getPhone();
        LambdaQueryWrapper<Parent> o = new LambdaQueryWrapper<>();
        o.eq(Parent::getPhone,phone);
        Parent parent = parentMapper.selectOne(o);
        Long id1 = parent.getId();
        LambdaQueryWrapper<ParentClass> o1 = new LambdaQueryWrapper<>();
        o1.eq(ParentClass::getClassId,id);
        o1.eq(ParentClass::getParentId,id1);
        parentClassMapper.delete(o1);
    }
}




